package dt

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"analysis-ss/models"
)

type Children struct {
	DataTable
}

func (cs *Children) GetTableInfo(start, length int, args ...string) error {
	centerId := ""
	teacherId := ""
	childName := ""
	argsLen := len(args)
	if argsLen > 1 {
		centerId = args[0]
		teacherId = args[1]
		childName = args[2]
	} else if argsLen == 1 {
		centerId = args[0]
	}

	// 起始位置和当前页面信息
	cs.Start = start
	cs.Length = length
	beego.Debug("中心ID :", centerId, "孩子名字 :", childName, "老师ID :", teacherId)
	if teacherId != "" {
		children := teacherStudentList(teacherId, childName, start, length)
		cs.Data = children
		count, _ := QueryteacherStudentListTotal(teacherId, childName)
		cs.RecordsTotal = count
		cs.RecordsFiltered = count
	} else {
		beego.Debug("centerId :", centerId, "childName :", childName, "start :", start, "length", length)
		children := centerStudentList(centerId, childName, start, length)
		cs.Data = children
		count, _ := QueryCenterChildTotal(centerId, childName)
		cs.RecordsTotal = count
		cs.RecordsFiltered = count

	}

	return nil
}

func QueryTeacherChild(teacherId, childName interface{}, start, length int) ([]models.Child, error) {
	var childs []models.Child
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `SELECT
                 distinct(c.id) as id,c.name,c.sex,c.grade,c.year,c.month,c.day
             FROM
                  t_child c
             LEFT JOIN
                   t_schedule_child sc ON c.id = sc.child_id
             LEFT JOIN
                   t_schedule s ON s.id = sc.schedule_id
              WHERE
                    c.is_delete != 1
                    AND sc.is_delete != 1
                    AND s.is_delete != 1
              AND s.teacher_id = ? `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  c.name like "%`, childName, `%" `)
	}

	sqlStr = fmt.Sprint(sqlStr, "  order by c.year asc ,CAST(c.month as SIGNED) asc,CAST(c.day as SIGNED) asc  limit ", start, ",", length)
	_, err := o.Raw(sqlStr, teacherId).QueryRows(&childs)
	return childs, err
}

func QueryCenterChild(centerId, childName interface{}, start, length int) ([]models.Child, error) {
	var childs []models.Child
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `SELECT
                  id ,name,sex,grade,year,month,day
             FROM
                  t_child
              WHERE
                   is_delete != 1 and center_id = ? `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  name like "%`, childName, `%" `)
	}
	sqlStr = fmt.Sprint(sqlStr, " order by year asc ,CAST(month as SIGNED) asc,CAST(day as SIGNED) asc  limit ", start, ",", length)
	_, err := o.Raw(sqlStr, centerId).QueryRows(&childs)
	return childs, err
}

// 获得老师进来的总页码
func QueryCenterChildTotal(centerId, childName interface{}) (int, error) {
	var Total int
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `SELECT
	               count(id)
             FROM
                  t_child
              WHERE
                   is_delete != 1 and center_id = ? `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  name like "%`, childName, `%" `)
	}
	err := o.Raw(sqlStr, centerId).QueryRow(&Total)
	return Total, err
}

// 获得老师进来的总页码
func QueryteacherStudentListTotal(teacherId, childName interface{}) (int, error) {
	var Total int
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `SELECT
                   count(distinct(c.id))
              FROM
                    t_child c
              LEFT JOIN
                     t_schedule_child sc ON c.id = sc.child_id
              LEFT JOIN
                     t_schedule s ON s.id = sc.schedule_id
              WHERE
                      c.is_delete != 1 AND sc.is_delete != 1  AND s.is_delete != 1
              AND s.teacher_id = ? `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  c.name like "%`, childName, `%" `)
	}
	err := o.Raw(sqlStr, teacherId).QueryRow(&Total)
	return Total, err
}

func queryChildrenCount(centerId, teacherName string) (int, error) {
	var count int
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
	           count(distinct(teacher_id))
                  from
                  t_schedule_child
                  where 1=1 `
	if centerId != "" && centerId != "0" {
		sqlStr = fmt.Sprint(sqlStr, ` and  center_id =`, centerId)
	}
	if teacherName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  teacher_name like "%`, teacherName, `%" `)
	}

	err := o.Raw(sqlStr).QueryRow(&count)
	return count, err
}

// 由中心去查学生页面
func centerStudentList(centerId, childName interface{}, start int, length int) []models.Child {
	//查询所有的中心
	children, err := QueryCenterChild(centerId, childName, start, length)
	if err != nil {
		beego.Debug(err)
	}
	beego.Debug(children)
	// 设置年级与年龄
	for i := 0; i < len(children); i++ {
		children[i].Age = untils.GetChildAge(children[i].Year, children[i].Month, children[i].Day)
		children[i].Grade = untils.GetchildGrade(children[i].Grade)
	}
	return children
}

// 由老师去查学生页面
func teacherStudentList(teacherId, childName interface{}, start int, length int) []models.Child {
	//查询所有的中心
	children, err := QueryTeacherChild(teacherId, childName, start, length)
	if err != nil {
		beego.Debug(err)
	}

	// 设置年级与年龄
	for i := 0; i < len(children); i++ {
		children[i].Age = untils.GetChildAge(children[i].Year, children[i].Month, children[i].Day)
		children[i].Grade = untils.GetchildGrade(children[i].Grade)
	}
	return children
}
